package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.UmsResourceMapper;
import com.shangcheng.model.domain.UmsResource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/13 19:12
 */@DubboService
public class UmsResourceApiImpl implements UmsResourceApi{
     @Autowired
     private UmsResourceMapper umsResourceMapper;
    /**
     * 添加后台资源
     * @param umsResource
     */
    @Override
    public void createResource(UmsResource umsResource) {
        umsResourceMapper.insert(umsResource);
    }

    /**
     * 根据id删除后台资源
     * @param id
     */
    @Override
    public void deleteResource(Integer id) {
        umsResourceMapper.deleteById(id);
    }

    /**
     * 分页模糊查询后台资源
     * @param pageNum
     * @param pageSize
     * @param resource
     * @return
     */
    @Override
    public IPage<UmsResource> resourceList(String pageNum, String pageSize, UmsResource resource) {
        IPage<UmsResource> page = new Page<>(Convert.toLong(pageNum),Convert.toLong(pageSize));
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(resource.getCategoryId()),UmsResource::getCategoryId,resource.getCategoryId());
        wrapper.like(StringUtils.hasText(resource.getName()),UmsResource::getName,resource.getName());
        wrapper.like(StringUtils.hasText(resource.getUrl()),UmsResource::getUrl,resource.getUrl());
        return umsResourceMapper.selectPage(page,wrapper);
    }

    /**
     * 查询所有后台资源
     * @return
     */
    @Override
    public List<UmsResource> allResourceList() {
        return umsResourceMapper.selectList(null);
    }

    /**
     * 修改后台资源
     * @param umsResource
     */
    @Override
    public void updateResource(UmsResource umsResource) {
        umsResourceMapper.updateById(umsResource);
    }

    /**
     * 根据id查询资源详情
     * @param id
     * @return
     */
    @Override
    public UmsResource findUmsResourceById(Integer id) {
        return umsResourceMapper.selectById(id);
    }

    /**
     * 根据资源名称查询资源
     * @param umsResource
     * @return
     */
    @Override
    public UmsResource findUmsResourceByName(UmsResource umsResource) {
        LambdaQueryWrapper<UmsResource> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(UmsResource::getName,umsResource.getName());
        return umsResourceMapper.selectOne(wrapper);
    }
}
